package rs.networx.servlets;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import rs.networx.dao.PersonDao;
import rs.networx.entity.Person;
import rs.networx.util.ErrorMessages;

public class EditPasswordServlet extends HttpServlet {

	private static final long serialVersionUID = 1457610626332202214L;
	
	@EJB
	private PersonDao personDao;

	private String username;
	private String password;
	private Person loggedUser;
	
	private String oldPassword;
	private String newPassword;
	private String confirmNewPassword;
	
	//--------------------------------------------------------------------------------------------------------------------------------------------------
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		
		System.out.println("LOADED: EditPasswordServlet");

		response.setContentType("text/html; charset=UTF-8");
		request.setCharacterEncoding(response.getCharacterEncoding());
		
		HttpSession session = request.getSession();
		loggedUser = (Person)session.getAttribute("logovaniUser");

		try {
			username = loggedUser.getEmail();
			password = loggedUser.getPassword();
			System.out.println(username + "  " + password);
			
			oldPassword = request.getParameter("OldPassword");
			newPassword = request.getParameter("NewPassword");
			confirmNewPassword = request.getParameter("ConfirmNewPassword");
			
			System.out.println("username: " + username + 
					"password: " + password +
					"oldPassword: " + oldPassword +
					"newPassword: " + newPassword + 
					"confirmNewPassword: " + confirmNewPassword);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
		
		// provere ispravnosti stare i nove lozinke...
		if(!password.equals(oldPassword)){
			request.setAttribute("editPasswordStatus", ErrorMessages.INCORRECT_OLD_PASSWORD);
			
			request.getRequestDispatcher("EditPassword.jsp").forward(request, response);
			return;
		}else if(!newPassword.equals(confirmNewPassword)){
			request.setAttribute("editPasswordStatus", ErrorMessages.INCORRECT_NEW_PASSWORD);
			
			request.getRequestDispatcher("EditPassword.jsp").forward(request, response);
			return;
		}
		
		loggedUser.setPassword(newPassword);
		
		// update podataka u bazi...
		personDao.merge(loggedUser);
		
		// i vracamo se na home page logovanog korisnika...
		response.sendRedirect("LoginServlet");
	}
	
	//--------------------------------------------------------------------------------------------------------------------------------------------------
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		doGet(request, response);
	}
	

}
